//适配
document.getElementsByTagName("html")[0].style.fontSize = document.body.clientWidth * 100 / 1080 + "px";
//接口
var HOST = 'http://echolife.xiaoxiao001.com';
var RENT = HOST + '/rent/detail';
var SECONDHAND = HOST + '/sechand/detail/detail';
var SCHOOLARTICLE = HOST + '/school/article/detail';
var JOY = HOST + '/joy/detail/';
var RESURANT = HOST + '/resturant/detail';
var STORE = HOST + '/store/detail';
var COMMENT= HOST + '/comment/list';
//评论类型
var TYPE_RESURANT = 2;
var TYPE_JOY = 3;
var TYPE_STORE = 4;
//最多显示评论数量
var commentNum = 3;
//二维码地址
var ERWEIMA = 'imgs/03.jpg';

$(document).ready((function () {
    var url = location.href;
    var id = getQueryStringArgs()['id'];
    if(url.indexOf('rentDetail.html') !== -1){
        getRentDetail(id,RENT);
    }
    if(url.indexOf('schoolArticleDetail.html') !== -1){
        getSchoolArticleDetail(id,SCHOOLARTICLE);
    }
    if(url.indexOf('secondHandDetail.html') !== -1){
        getSecondHandDetail(id,SECONDHAND);
    }
    if(url.indexOf('resturantDetail.html') !== -1){
        getStoreJoyResurantDetail(id,RESURANT,TYPE_RESURANT);
    }
    if(url.indexOf('joyDetail.html') !== -1){
        getStoreJoyResurantDetail(id,JOY,TYPE_JOY);
    }
    if(url.indexOf('storeDetail.html') !== -1){
        getStoreJoyResurantDetail(id,STORE,TYPE_STORE);
    }
    //修改二维码
    $('.erweima img').attr('src',ERWEIMA);
}));
//获取参数id
function getQueryStringArgs(){
    var qs = (location.search.length > 0) ? (location.search.substring(1)) : " ";
    items = (qs.length > 0) ? qs.split("&") :[ ];
    item = null;
    name = null;
    value = null;
    args = [];
    for(var i = 0; i<items.length; i++){
        item = items[i].split("=");
        name = item[0];
        value = item[1];
        if(name.length){
            args[name] = value;
        }
    }
    return args;
}
//获取租房信息
function getRentDetail(id,url){
    $.get(url+'?id='+id,function(result){
        $('.title').text(result.result.title);
        $('#userHeadBoxName').text(result.result.username);
        $('#userHeadBoxImg').attr('src',result.result.avatarInfo[0].url);
        $('#publishTime').text(result.result.create_time);
        $('#sum').text(result.result.view_number);
        $('#type').text(result.result.house_type);
        $('#typeRoom').text(result.result.bedroom_num);
        $('#typeChuNum').text(result.result.kitchen_share+'人');
        $('#typeWeiNum').text(result.result.bathroom_share+'人');
        $('#typeYear').text(result.result.beginDate+'-');
        $('#endDate').text(result.result.endDate);
        $('#typeCode').text(result.result.express_no);
        $('#prefileContent').text(result.result.comment);

        swiper(result);
    },'json')

}
//获取文章信息
function getSchoolArticleDetail(id,url) {
    $.get(url+'?id='+id,function (result) {
        $('.title').text(result.result.title);
        $('#userHeadBoxName').text(result.result.username);
        $('#userHeadBoxImg').attr('src',result.result.avatar_info[0].url);
        $('#prefileContent').text(result.result.comment);
        var imgs = result.result.image_info;
        var img = null;
        for(var i = 0;i<imgs.length;i++){
            img = new Image(imgs[i].url);
            if(img.width<img.height){
                $('.contentImgBox').append(
                    '<div class="contentImgV">'+
                    '<img src="'+imgs[i].url+'">'+
                    '</div>')
            }else{
                $('.contentImgBox').append(
                    '<div class="contentImgH">'+
                    '<img src="'+imgs[i].url+'">'+
                    '</div>')
            }
            img = null;
        }


    },'json')
}
//获取商品信息
function getSecondHandDetail(id,url) {
    $.get(url+'?id='+id,function (result) {
        $('.title').text(result.result.title);
        $('#userHeadBoxName').text(result.result.username);
        $('#userHeadBoxImg').attr('src',result.result.avatarInfo[0].url);
        $('.contentTitle').text(result.result.cname);
        $('#money').text(result.result.price);
        $('#publishTime').text(result.result.create_time);
        $('#sum').text(result.result.view_number);
        $('#typeCode').text(result.result.express_no);
        $('#prefileContent').text(result.result.comment);

        swiper(result);
    },'json')
}
//获取商铺、娱乐和餐厅信息
function getStoreJoyResurantDetail(id,url,type) {
    $.get(url+'?id='+id,function (result) {
        $('.title').text(result.result.title);
        $('#userHeadBoxName').text(result.result.username);
        $('#userHeadBoxImg').attr('src',result.result.avatarInfo[0].url);
        $('#resturantType').text(result.result.cname);
        $('#sum').text(result.result.view_number);
        $('#typeCode').text(result.result.express_no);
        $('#address').text(result.result.address);
        $('#prefileContent').text(result.result.comment);

        swiper(result);
        getComment(type,result.result.user_id,result.result.id,result.result.username);
    },'json')
}
//轮播组件
function swiper(result){
    //banner
    var image_urls = [];
    var imgs_info = result.result.image_info;
    for(var i = 0;i < imgs_info.length;i++){
        image_urls.push(imgs_info[i].url)
    }
    image_urls.forEach(function(url){
        $('.wrapper1').append('<div class="swiper-slide">'+
            '<img src="'+url+'">'+
            '</div>');
    });
    var mySwiper = new Swiper ('.container1', {
        direction: 'horizontal',
        loop: true,
        pagination: '.swiper-pagination',
        autoplay:3000
    });
    //AD
    // var ad_image_urls = [];
    // var ad_imgs_info = result.result.image_info;
    // for(var i = 0;i < imgs_info.length;i++){
    //     image_urls.push(imgs_info[i].url)
    // }
    // image_urls.forEach(function(url){
        //$('.wrapper2').append('<div class="swiper-slide">'+
        //    '<img src="'+url+'">'+
        //    '</div>');
    // });
    var mySwiper2 = new Swiper ('.container2', {
        direction: 'horizontal',
        loop: true,
        pagination: '.swiper-pagination',
        autoplay:3000
    })
}
//获取评论
function getComment(type,user_id,id,name) {
    $.get(COMMENT+'?type='+type+'&user_id='+user_id+'&id='+id,function (result) {
        var data = result.result.data;
        var leng = data.length;
        commentNum = leng<commentNum?leng:commentNum;
        for(var i=0;i<commentNum;i++){
            if(data[i].relpy != null){
                $('.feedback ul').append(
                '<li>'+
                '<div class="userImg">'+
                '<img src="'+data[i].avatar_info.url+'">'+
                '</div>'+
                '<div class="feedbackContent">'+
                '<span class="userName">'+data[i].username+'</span>'+
                '<div class="userContent">'+data[i].comment+'</div>'+
                '<div class="feedbackTime">'+data[i]+create_time+'</div>'+
                '<div class="userFeedback">'+
                '<span class="reName">'+name+'</span>'+
                '<div class="reContent">'+data[i].relpy+'</div>'+
                '</div>'+
                '</div>'+
                '</li>'
                )
            }else{
                $('.feedback ul').append(
                '<li>'+
                '<div class="userImg">'+
                '<img src="'+data[i].avatar_info.url+'">'+
                '</div>'+
                '<div class="feedbackContent">'+
                '<span class="userName">'+data[i].username+'</span>'+
                '<div class="userContent">'+data[i].comment+'</div>'+
                '<div class="feedbackTime">'+data[i].create_time+'</div>'+
                '</div>'+
                '</li>'
                );
            }
        }
    },'json')
}
